﻿using BTv2.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TechNewLogic.GraphIT.Hv;
using TechNewLogic.GraphIT.Hv.Legend;
using TechNewLogic.GraphIT.Hv.Vertical;
using TechNewLogic.GraphIT.Printing;

namespace BTv2.UserControls
{
    /// <summary>
    /// Interaction logic for GraphTile.xaml
    /// </summary>
    public partial class GraphTile : UserControl
    {
        private MainWindow _mw;

        public GraphTile()
        {
            InitializeComponent();
        }

        public GraphTile(MainWindow mw)
        {
            InitializeComponent();
            _mw = mw;
        }

        private void Grraph_Loaded_Event(object sender, RoutedEventArgs e)
        {
            //Changes the bacakgraound of the graph field
            CurveDisplay.GeneralBackground = new SolidColorBrush(Color.FromRgb(0xff, 0xff, 0xff));

            //Change the color of the text, cursors etc.
            CurveDisplay.GeneralForeground = new SolidColorBrush(Color.FromRgb(0, 0, 0));


            // Set the bounds of the time axis
            CurveDisplay.TimeDoublePlottingSystem.TimeAxis.SetBounds(
               new DateTime(2013,4,1),
               new DateTime(2013,4,12));

           
            // Initialize the table legend and add it to the page
            var tableLegend = new TableLegend(
                CurveDisplay,
                "Term",
                "Location",
                "Aggregate",
                "Medium",
                "Measurement");

            //tableLegendHolder.Content = tableLegend;

            // Initialize the tile legend and add it to the page
            var tileLegend = new TileLegend(CurveDisplay);
            tileLegend.Width = 200;
            tileLegend.TileWidth = 200;
           
            tileLegendHolder.Content = tileLegend;

            var sensors = SensorsController.GetInstance().Sensors;

            var data = DatabaseController.GetInstance().GetSensorData(sensors.ElementAt(0), new DateTime(2013, 4, 1), new DateTime(2013, 4, 12));
            data = DatabaseController.GetInstance().GetSensorData(sensors.ElementAt(1), new DateTime(2013, 4, 1), new DateTime(2013, 4, 12));
          
            TimeDoubleCurve _curve;

            TimeDoubleCurve _curve1;

			// Create a new curve
			_curve = CurveDisplay.TimeDoublePlottingSystem.AddCurve(
				sensors.ElementAt(0).MUnit, 5, 40, Colors.Black, RedrawTime.Ms500, AxisMatchingMode.UomAndBounds, 
				CurveDrawingMode.SimpleLine(),
				new FloatingCommaValueFormater(),
				new InterpolationValueFetchStrategy(),
				AxisFormat.Double,
				int.MaxValue);

            // Create a new curve
            _curve1 = CurveDisplay.TimeDoublePlottingSystem.AddCurve(
                sensors.ElementAt(1).MUnit, 5, 40, Colors.Green, RedrawTime.Ms500, AxisMatchingMode.UomAndBounds,
                CurveDrawingMode.SimpleLine(),
                new FloatingCommaValueFormater(),
                new InterpolationValueFetchStrategy(),
                AxisFormat.Double,
                int.MaxValue);

			// We have to set some description for the curve so that it can be displayed in the table legend!
			_curve.Description.DescriptionText1 = sensors.ElementAt(0).Name;
			_curve.Description.DescriptionText2 = "X";
			_curve.Description.DescriptionText3 = "Y";
			_curve.Description.DescriptionText4 = "Z";
			_curve.Description.DescriptionText5 = "1";

            // We have to set some description for the curve so that it can be displayed in the table legend!
            _curve1.Description.DescriptionText1 = sensors.ElementAt(1).Name;
            _curve1.Description.DescriptionText2 = "X";
            _curve1.Description.DescriptionText3 = "Y";
            _curve1.Description.DescriptionText4 = "Z";
            _curve1.Description.DescriptionText5 = "2";

            //Attach the curve
            var atadata = new List<TimeDoubleDataEntry>();
            foreach (var dataPoint in sensors.ElementAt(0).DataSet.Measurments)
            {
                atadata.Add(new TimeDoubleDataEntry(dataPoint.TimeStamp, dataPoint.Value));
            }

            //Attach the curve
            var atadata1 = new List<TimeDoubleDataEntry>();
            foreach (var dataPoint in sensors.ElementAt(1).DataSet.Measurments)
            {
                atadata1.Add(new TimeDoubleDataEntry(dataPoint.TimeStamp, dataPoint.Value));
            }


            _curve.DataSeries.Append(atadata);
            _curve1.DataSeries.Append(atadata1);
		
        }

        private void ExportPDFClicked(object sender, RoutedEventArgs e)
        {
            var dummyTb = new TextBlock
            {
                Text = "Date: " + DateTime.Now,
                Margin = new Thickness(20),
                FontSize = 20,
                TextAlignment = TextAlignment.Center
            };
            CurveDisplay.ExportPDFFile(30, new PrintContent(dummyTb, Dock.Top));
        }

        private void PrintClicked(object sender, RoutedEventArgs e)
        {
            var dummyTb = new TextBlock
            {
                Text = "Date: " + DateTime.Now,
                Margin = new Thickness(20),
                FontSize = 20,
                TextAlignment = TextAlignment.Center
            };
            CurveDisplay.ShowPrintPreview(new PrintContent(dummyTb, Dock.Top));
        }

        private void ToggleAdvanced(object sender, RoutedEventArgs e)
        {
            ButtonAdvanceToggle.Click -= ToggleAdvanced;
            ButtonAdvanceToggle.Click += ToggleSimple;
            ButtonAdvanceToggle.ToolTip = "Toggle Normal View";
            ButtonAdvanceToggle.SetResourceReference(ContentPresenter.ContentProperty, "NormalPath");
            Grid.SetColumnSpan(CurveDisplay, 2);
        }
        private void ToggleSimple(object sender, RoutedEventArgs e)
        {
            ButtonAdvanceToggle.ToolTip = "Toggle Advanced View";
            ButtonAdvanceToggle.Click += ToggleAdvanced;
            ButtonAdvanceToggle.Click -= ToggleSimple;
            ButtonAdvanceToggle.SetResourceReference(ContentPresenter.ContentProperty, "AdvancedPath");
            Grid.SetColumnSpan(CurveDisplay, 3);
        }

        private void ShowSensorGroups(object sender, RoutedEventArgs e)
        {
            SensorGroupsMenu.IsOpen = true;
          //  var rrr = new Office2007BlackResourceDictionary();
         //   Office2007SilverResourceDictionary ttt = new Office2007SilverResourceDictionary();
        }
    }
}
